﻿using LibraryInterface;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Test.BooksCRUD
{
    [TestClass()]
    public class LibraryInterfaceTest
    {
        [TestMethod()]
        [DeploymentItem("LibraryInterface.exe")]
        public void CreateBookTest()
        {
            LibraryEntities lbEntity = new LibraryEntities();
            string name = "Koshmarni Detski Prikazki";
            int isbn = 1234567;
            string type = "textbook";
            Book book = new Book();
            book.Name = name;
            book.ISBN = isbn;
            book.Type = type;
            try
            {
                LibraryInterface_Accessor.CreateBook(name, isbn, type, lbEntity);
                Book searched = lbEntity.Books.First<Book>(b => b.Name == name && b.ISBN == isbn && b.Type == type);
                Assert.AreNotEqual(searched, null);
            }
            finally
            {
                Book searched = lbEntity.Books.First<Book>(b => b.Name == name && b.ISBN == isbn && b.Type == type);
                lbEntity.Books.DeleteObject(searched);
                lbEntity.SaveChanges();
            }
        }

        [TestMethod()]
        [DeploymentItem("LibraryInterface.exe")]
        public void ReadBookTest()
        {
            LibraryEntities lbEntity = new LibraryEntities();
            string name = "Koshmarni Detski Prikazki";
            int isbn = 1234567;
            string type = "textbook";
            Book book = new Book();
            book.Name = name;
            book.ISBN = isbn;
            book.Type = type;
            StringWriter text = new StringWriter();
            Console.SetOut(text);
            try
            {
                lbEntity.Books.AddObject(book);
                lbEntity.SaveChanges();
                Book searched = lbEntity.Books.First<Book>(b => b.Name == name && b.ISBN == isbn && b.Type == type);
                int id = searched.BookID;
                LibraryInterface_Accessor.ReadBook(id, lbEntity);
                string actualText = text.ToString();
                string expectedText = "Name: Koshmarni Detski Prikazki\n" +
                        "ISBN: 1234567\r\n";
                Assert.AreEqual(expectedText, actualText);
            }
            finally
            {
                Book searched = lbEntity.Books.First<Book>(b => b.Name == name && b.ISBN == isbn && b.Type == type);
                lbEntity.Books.DeleteObject(searched);
                lbEntity.SaveChanges();
            }
        }

        [TestMethod()]
        [DeploymentItem("LibraryInterface.exe")]
        public void UpdateBookTest()
        {
            LibraryEntities lbEntity = new LibraryEntities();
            string name = "Koshmarni Detski Prikazki";
            int isbn = 1234567;
            string type = "textbook";
            Book book = new Book();
            book.Name = name;
            book.ISBN = isbn;
            book.Type = type;

            string newName = "Priatni Detski Prikazki";
            int newIsbn = 7654321;
            string newType = "textbook";

            try
            {
                lbEntity.Books.AddObject(book);
                lbEntity.SaveChanges();
                Book searched = lbEntity.Books.First<Book>(b => b.Name == name && b.ISBN == isbn && b.Type == type);
                int id = searched.BookID;
                LibraryInterface_Accessor.UpdateBook(id, newName, newIsbn, newType, lbEntity);
                searched = lbEntity.Books.First<Book>(b => b.BookID == id);
                Assert.AreEqual(searched, book);
            }
            finally
            {
                Book searched = lbEntity.Books.First<Book>(b => b.Name == newName && b.ISBN == newIsbn && b.Type == newType);
                lbEntity.Books.DeleteObject(searched);
                lbEntity.SaveChanges();
            }
        }

        [TestMethod()]
        [DeploymentItem("LibraryInterface.exe")]
        public void DeleteBookTest()
        {
            LibraryEntities lbEntity = new LibraryEntities();
            string name = "Koshmarni Detski Prikazki";
            int isbn = 1234567;
            string type = "textbook";
            Book book = new Book();
            book.Name = name;
            book.ISBN = isbn;
            book.Type = type;

            try
            {
                lbEntity.Books.AddObject(book);
                lbEntity.SaveChanges();
                Book searched = lbEntity.Books.First<Book>(b => b.Name == name && b.ISBN == isbn && b.Type == type);
                int id = searched.BookID;
                LibraryInterface_Accessor.DeleteBook(id, lbEntity);
                bool actual = lbEntity.Books.Any(b => b.BookID == id);
                bool expected = false;
                Assert.AreEqual(expected, actual);
            }
            finally
            {
                if (lbEntity.Books.Any(b => b.Name == name && b.ISBN == isbn && b.Type == type))
                {
                    Book searched = lbEntity.Books.First<Book>(b => b.Name == name && b.ISBN == isbn && b.Type == type);
                    lbEntity.Books.DeleteObject(searched);
                    lbEntity.SaveChanges();
                }
            }
        }
    }
}
